package moxl.parser.assemblers;

import java.util.Formatter;

import moxl.reader.Location;
import moxl.tokenizer.Token;

public abstract class AssemblerStateException extends AssemblerException
{
	public AssemblerStateException(Location location)
	{
		super("Unexpected input", location);
	}

	@Override
	public abstract String getMessage();

	protected String createMessage(String state, Token token)
	{
		Formatter formatter = new Formatter();
		if (token.getImage().trim().length() > 0)
		{
			return formatter.format("Unexpected input: %s (type is %s) in state %s at location %s", token.getImage(), token.getType(), state,
			        token.getLocation()).toString();
		}
		else
		{
			StringBuilder decodedWhitespace = new StringBuilder();
			for (char c : token.getImage().toCharArray())
			{
				decodedWhitespace.append(' ');
				String hexString = Integer.toHexString(c).toUpperCase();
				if (hexString.length() == 1)
				{
					decodedWhitespace.append('0');
				}
				decodedWhitespace.append(hexString);
			}
			return formatter.format("Unexpected whitespace:%s (type is %s) in state %s at location %s", decodedWhitespace, token.getType(), state,
			        token.getLocation()).toString();
		}
	}
}
